package com.detooa.detooa.service.impl;

import java.util.Date;
import java.util.List;
import com.detooa.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.detooa.detooa.mapper.DeToOaConfigMapper;
import com.detooa.detooa.mapper.DeToOaRecordMapper;
import com.detooa.detooa.config.DataEaseConfig;
import com.detooa.detooa.domain.DeToOaConfig;
import com.detooa.detooa.domain.DeToOaRecord;
import com.detooa.detooa.service.IDeToOaConfigService;
import com.detooa.detooa.utils.DeUtils;
import com.detooa.detooa.utils.OaUtils;
import com.detooa.detooa.utils.bean.de.response.BaseDeVo;
import com.detooa.detooa.utils.bean.de.response.DeLinkVo;
import com.detooa.detooa.utils.bean.oa.request.OaMessageVo;
import com.detooa.detooa.utils.bean.oa.response.OaMessageResultVo;

import lombok.extern.log4j.Log4j2;

import com.detooa.common.core.text.Convert;

/**
 * 推送配置信息Service业务层处理
 * 
 * @author detooa
 * @date 2024-01-17
 */
@Service("deToOaConfigService")
@Log4j2
public class DeToOaConfigServiceImpl implements IDeToOaConfigService {

    @Autowired
    private DeToOaConfigMapper deToOaConfigMapper;

    @Autowired
    private DeToOaRecordMapper deToOaRecordMapper;

    /**
     * 查询推送配置信息
     * 
     * @param configId 推送配置信息主键
     * @return 推送配置信息
     */
    @Override
    public DeToOaConfig selectDeToOaConfigByConfigId(Long configId)
    {
        return deToOaConfigMapper.selectDeToOaConfigByConfigId(configId);
    }

    /**
     * 查询推送配置信息列表
     * 
     * @param deToOaConfig 推送配置信息
     * @return 推送配置信息
     */
    @Override
    public List<DeToOaConfig> selectDeToOaConfigList(DeToOaConfig deToOaConfig)
    {
        return deToOaConfigMapper.selectDeToOaConfigList(deToOaConfig);
    }

    /**
     * 新增推送配置信息
     * 
     * @param deToOaConfig 推送配置信息
     * @return 结果
     */
    @Override
    public int insertDeToOaConfig(DeToOaConfig deToOaConfig)
    {
        deToOaConfig.setCreateTime(DateUtils.getNowDate());
        return deToOaConfigMapper.insertDeToOaConfig(deToOaConfig);
    }

    /**
     * 修改推送配置信息
     * 
     * @param deToOaConfig 推送配置信息
     * @return 结果
     */
    @Override
    public int updateDeToOaConfig(DeToOaConfig deToOaConfig)
    {
        deToOaConfig.setUpdateTime(DateUtils.getNowDate());
        return deToOaConfigMapper.updateDeToOaConfig(deToOaConfig);
    }

    /**
     * 批量删除推送配置信息
     * 
     * @param configIds 需要删除的推送配置信息主键
     * @return 结果
     */
    @Override
    public int deleteDeToOaConfigByConfigIds(String configIds)
    {
        return deToOaConfigMapper.deleteDeToOaConfigByConfigIds(Convert.toStrArray(configIds));
    }

    /**
     * 删除推送配置信息信息
     * 
     * @param configId 推送配置信息主键
     * @return 结果
     */
    @Override
    public int deleteDeToOaConfigByConfigId(Long configId)
    {
        return deToOaConfigMapper.deleteDeToOaConfigByConfigId(configId);
    }

    @Override
    public void pushOa(String configIds)
    {
        String[] ids = Convert.toStrArray(configIds);
        for (String configId : ids) {
            DeToOaRecord deToOaRecord = new DeToOaRecord();
            try {
                DeToOaConfig config = this.selectDeToOaConfigByConfigId(Long.valueOf(configId));
                // 根据资源ID获取信息
                DeLinkVo deLinkVo = DeUtils.currentGenerate(config.getResourceId());
                // 开启分享的才推送
                if (deLinkVo.getValid()) {
                    BaseDeVo baseDeVo = DeUtils.shortUrl(config.getResourceId());// 获取短连接
                    String shortUrl = (String) baseDeVo.getData();
                    String context = config.getContext();
                    context = context.replace("#{shortUrl}", DataEaseConfig.deBaseUrl + shortUrl);
                    // 是否启用密码
                    if (deLinkVo.getEnablePwd() && null != deLinkVo.getPwd()) {
                        context = context.replace("#{pwd}", "密码：" + deLinkVo.getPwd());
                    } else {
                        context = context.replace("#{pwd}", "");
                    }
                    if (null != deLinkVo.getOverTime()) {
                        context = context.replace("#{overTime}", "过期时间：" + deLinkVo.getOverTimeFmt());
                    } else {
                        context = context.replace("#{overTime}", "");
                    }
                    OaMessageVo messageVo = new OaMessageVo(config.getLoginIdList(), config.getTitle(), context, DataEaseConfig.deBaseUrl + shortUrl,
                            DataEaseConfig.deWwwBaseUrl + shortUrl);
                    log.info(messageVo.toString());

                    deToOaRecord.setContext(context);
                    deToOaRecord.setCreateTime(new Date());
                    deToOaRecord.setResourceId(config.getResourceId());
                    deToOaRecord.setResourceName(config.getResourceName());
                    deToOaRecord.setLoginIdList(config.getLoginIdList());
                    deToOaRecord.setTitle(config.getTitle());
                    deToOaRecord.setStatus("1");// 推送成功
                    OaMessageResultVo resultVo = OaUtils.sendCustomMessage(messageVo);// 向OA推送消息
                    if (!resultVo.getStatus()) {
                        deToOaRecord.setStatus("2");// 推送失败
                        throw new RuntimeException("OA消息发送失败");
                    }
                } else {
                    deToOaRecord.setStatus("2");// 推送失败
                    deToOaRecord.setRemark("DateEase该看板已关闭分享链接");
                }
            } catch (NumberFormatException e) {
                log.error("配置ID{}错误，应该为long型数字", configId);
                deToOaRecord.setStatus("2");// 推送失败
                deToOaRecord.setRemark("配置ID{" + configId + "}错误，应该为long型数字");
                e.printStackTrace();
            } catch (Exception e) {
                deToOaRecord.setStatus("2");// 推送失败
                deToOaRecord.setRemark(e.getLocalizedMessage());
                e.printStackTrace();
            } finally {
                deToOaRecordMapper.insertDeToOaRecord(deToOaRecord);
            }
        }
    }
}
